package com.example.admintor.threehome;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

/**
 * Created by Admintor on 2017/12/5.
 */

public class ScaleButton extends ImageView {
    private Context context;
    private TypedArray t;
    private String text;

    public ScaleButton(Context context) {
        this(context, null);
        this.context = context;
    }

    public ScaleButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        this.context = context;
        init(attrs);
    }

    public ScaleButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(attrs);
    }

    public void init(AttributeSet attrs) {
        t = getContext().obtainStyledAttributes(attrs, R.styleable.ScaleButton);
        text = t.getString(R.styleable.ScaleButton_textValue);
        t.recycle();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                beginScale(R.animator.zoom_in);
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
                beginScale(R.animator.zoom_out);
                break;
            case MotionEvent.ACTION_CANCEL:
                beginScale(R.animator.zoom_out);
                break;
        }
        return true;
    }

    private synchronized void beginScale(int animation) {
        Animation an = AnimationUtils.loadAnimation(context, animation);
        an.setDuration(180);
        an.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                switch (text) {
                    case "one":
                        ScaleButton.this.setBackgroundResource(R.drawable.hj_down);
                        break;
                    case "two":
                        ScaleButton.this.setBackgroundResource(R.drawable.kz_down);
                        break;
                    case "three":
                        ScaleButton.this.setBackgroundResource(R.drawable.yp_down);
                        break;
                    case "four":
                        ScaleButton.this.setBackgroundResource(R.drawable.af_down);
                        break;
                }
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        an.setFillAfter(true);
        this.startAnimation(an);
    }
}